<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%3;
$name 		= $_REQUEST['name'];
$user 		= $_REQUEST['user'];
$title 		= $_REQUEST['title'];
$text[0] 	= "დავალებები პასუხისმგებელი პირების მიხედვით";
$text[1] 	= "'$type'- სტატუსების მიხედვით";
$text[2] 	= "'$type'-'$category'-ზარის ტიპის მიხედვით";

if ($category=="გადაცემულია გასარკვევად") $c=0;
elseif ($category=="გარკვევის პროცესშია") $c=1;
elseif ($category=="მოგვარებულია") $c=2;
elseif ($category=="დასრულებული") $c=3;

//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		if ($user==0){
		$result = mysql_query("	SELECT `status`.`call_status` AS category,
                        				COUNT(*),
                        				CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
                        					FROM 	task
                        					LEFT JOIN users ON task.responsible_user_id=users.id
                        					LEFT JOIN persons ON users.person_id = persons.id
                        					LEFT JOIN `status` ON task.`status`=`status`.id
                        					
                        					WHERE (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end'
                        					)*100,2),'%') AS percent
                        		
                                FROM 	task
                                LEFT JOIN users ON task.responsible_user_id=users.id
                                LEFT JOIN persons ON users.person_id = persons.id
                                LEFT JOIN `status` ON task.`status`=`status`.id
                                WHERE (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end'
                                GROUP BY category");
		}else 
		{
			$result = mysql_query("	SELECT `status`.`call_status` AS category,
                        				COUNT(*),
                        				CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
                        					FROM 	task
                        					LEFT JOIN users ON task.responsible_user_id=users.id
                        					LEFT JOIN persons ON users.person_id = persons.id
                        					LEFT JOIN `status` ON task.`status`=`status`.id
                        					
                        					WHERE (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND task.responsible_user_id=$user
                        					)*100,2),'%') AS percent
                        		
                                FROM 	task
                                LEFT JOIN users ON task.responsible_user_id=users.id
                                LEFT JOIN persons ON users.person_id = persons.id
                                LEFT JOIN `status` ON task.`status`=`status`.id
                                WHERE (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND task.responsible_user_id=$user
                                GROUP BY category");
		}
		$text[0]=$text[1];
	break;
	case 2:
		if ($user==0){
			$result = mysql_query("SELECT `category`.`name` AS category,
                                    COUNT(*),
                                    CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
                                    FROM 	task
                                    LEFT JOIN users ON task.responsible_user_id=users.id
                                    LEFT JOIN persons ON users.person_id = persons.id
                                    LEFT JOIN category ON task.call_type_id=category.id
                                    WHERE task.`status`=$c AND(persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end'
                                    )*100,2),'%') AS percent
                                    
                                    FROM 	task
                                    LEFT JOIN users ON task.responsible_user_id=users.id
                                    LEFT JOIN persons ON users.person_id = persons.id
                                    LEFT JOIN category ON task.call_type_id=category.id
                                    WHERE task.`status`=$c AND (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end'
                                    GROUP BY category");
		}else 
		{
			$result = mysql_query("SELECT `category`.`name` AS category,
                                    COUNT(*),
                                    CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*)
                                    FROM 	task
                                    LEFT JOIN users ON task.responsible_user_id=users.id
                                    LEFT JOIN persons ON users.person_id = persons.id
                                    LEFT JOIN category ON task.call_type_id=category.id
                                    WHERE task.`status`=$c AND(persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND task.responsible_user_id=$user
                                    )*100,2),'%') AS percent
                                    
                                    FROM 	task
                                    LEFT JOIN users ON task.responsible_user_id=users.id
                                    LEFT JOIN persons ON users.person_id = persons.id
                                    LEFT JOIN category ON task.call_type_id=category.id
                                    WHERE task.`status`=$c AND (persons.`name`='$type') and DATE(task.date) >= '$start' and  DATE(task.date) <= '$end' AND task.responsible_user_id=$user
                                    GROUP BY category");
		}
		$text[0]=$text[2];
	break;
	default:
		if ($user==0){
		$result = mysql_query("SELECT   persons.`name` as type,
                            			COUNT(*),
                            			CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM task
                            			JOIN users ON task.responsible_user_id=users.id
                            			WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end')*100,2),'%')
                                FROM task
                                JOIN users ON task.responsible_user_id=users.id
                                JOIN persons ON users.person_id = persons.id
                                WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end'
                                GROUP BY 	persons.`name`");
		
		}else{
			$result = mysql_query("SELECT   persons.`name` as type,
                            			COUNT(*),
                            			CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM task
                            			JOIN users ON task.responsible_user_id=users.id
                            			WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end' AND task.responsible_user_id=$user)*100,2),'%')
                                FROM task
                                JOIN users ON task.responsible_user_id=users.id
                                JOIN persons ON users.person_id = persons.id
                                WHERE DATE(task.date) >= '$start' AND DATE(task.date) <= '$end' AND task.responsible_user_id=$user
                                GROUP BY 	persons.`name`");
		}
		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
	case 'get_in_page':
		mysql_query("SET @i = 0;");
	  	$rResult = mysql_query("SELECT	`task`.id,
				`task`.id,
				`task`.`date`,
				`task`.`phone` AS `phone`,
				`c_type`.`name`  AS `call_type`,
				`cat`.`name`  AS `category`,
				`sub_cat`.`name`  AS `sub_category`,
				`object`.`name`  AS object,
				`persons`.`name`,
				`status`.`call_status`
FROM 	    `task`
LEFT JOIN 	`incomming_call` ON task.incomming_call_id=incomming_call.id
LEFT JOIN 	`category` AS c_type ON task.call_type_id=c_type.id
LEFT JOIN 	`category` AS cat ON task.category_id=cat.id
LEFT JOIN 	`category` AS sub_cat ON task.subcategory_id=sub_cat.id
LEFT JOIN 	`object` ON task.object_id=object.id
LEFT JOIN 	`status` ON	task.`status`=`status`.`id`
JOIN        `users` ON task.responsible_user_id = users.id
JOIN        `persons` ON users.person_id = persons.id
WHERE 		DATE(task.date) >= '$start' AND DATE(task.date) <= '$end' and `task`.`actived`=1 AND task.`status`=$c AND persons.`name`='$type' AND c_type.`name` = '$_REQUEST[rid]'");
	  
$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[] = $aRow[$i];
				$a=$aRow;
				
				{
				
				}
			}
			$data['aaData'][] = $row;
		}
		echo json_encode($data); return 0;
		break;
	default :
		echo "Action Is Null!";
		break;

}



?>